﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SSPharma.Common;
using System.Data;
using SSPharma.Biz.NhanSu;
using System.Collections.ObjectModel;
using SSPharma.Model;
using WpfCustomControlLibrary;

namespace SSPharma.View.NhanSu
{
    /// <summary>
    /// Interaction logic for ucDS_NhanSu.xaml
    /// </summary>
    public partial class ucDS_NhanSu : UserControl
    {
        #region valible
        private DataTable p_tListData, p_tDataCauTruc;
        private BizDSNhanSu _BizDSNhanSu;
        #endregion

        #region contructor
        public ucDS_NhanSu()
        {
            InitializeComponent();
            _BizDSNhanSu = new BizDSNhanSu();
        }

        #endregion

        #region Event
        /// <summary>
        /// User: TAMNN 16/08/2014
        ///  Mục đích: Sự kiện load dữ liệu của usercontroll
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //lấy format grid
            GetFormatGrid(txtFormName.Text);
            // gán điều kiện tìm kiếm
            cboDieuKien.ItemsSource = new ObservableCollection<cboCommon>(_BizDSNhanSu.fl_DieuKienTimKiem());
            cboDieuKien.SelectedIndex = 0;
        }
        /// <summary
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void g_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        }
        /// <summary>
        /// User: TAMNN 14/08/2014
        /// Mục đích: đánh số thứ tự cho lưới
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void g_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }
        /// <summary>
        /// User: TAMNN 16/08/2014
        /// Mục đích: select phòng ban load danh sách theo phòng ban
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvDanhMuc_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
        {
            var dr = tvDanhMuc.SelectedItem;
            string DepartId = dr.GetType().GetProperty("DepartId").GetValue(dr, null).ToString();
            string OrgId = dr.GetType().GetProperty("ORGID").GetValue(dr, null).ToString();
            this.GetDataResponseMenu(DepartId,OrgId);

        }
        /// <summary>
        /// User: TAMNN 16/08/2014
        /// Tìm kiếm nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTimKiem_Click(object sender, RoutedEventArgs e)
        {
            var dr = tvDanhMuc.SelectedItem;
            string TimKiem = string.Empty;
            string DepartId = dr.GetType().GetProperty("DepartId").GetValue(dr, null).ToString();
            string OrgId = dr.GetType().GetProperty("ORGID").GetValue(dr, null).ToString();
            IList<string> listValues = this.GetDataSearch();
            if (cboDieuKien.SelectedValue.ToString() == "HoTen")
            {
                TimKiem = "MaNhanVien like N'%" + listValues[0] + "%'";
            }
            else if (cboDieuKien.SelectedValue.ToString() == "NgaySinh")
            {
                TimKiem = "CONVERT(VARCHAR(20),NgaySinh,103) Like N'" + listValues[0] + "'";
            }
            else
            {
                TimKiem = "MaNhanVien like N'%" + listValues[0] + "%'";
            }
            this.GetDataResponseMenu(DepartId, OrgId,TimKiem);
        }
        /// <summary>
        /// User: TAMNN 16/08/2014
        /// Mục đích: chọn điều kiện tìm kiếm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboDieuKien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (cboDieuKien.SelectedValue.ToString() =="NgaySinh")
            {
                stpValues.Children.Clear();
                Label lblTu = new Label();
                lblTu.Name = "lblTu";
                lblTu.Content = "Chọn ngày: ";
                lblTu.Height = 25;
                lblTu.Width = 70;
                AWDatePicker dptTu = new AWDatePicker();
                dptTu.Height = 25;
                dptTu.Width = 120;
                dptTu.Tag = "DATA";
                stpValues.Children.Add(lblTu);
                stpValues.Children.Add(dptTu);
                dptTu.Focus();
                dptTu.TabIndex = 3;
            }
            else
            {
                stpValues.Children.Clear();
                Label lblTu = new Label();
                lblTu.Width = 10;
                TextBox txtSearch = new TextBox();
                txtSearch.Height = 24;
                txtSearch.Width = 250;
                txtSearch.Tag = "DATA";
                stpValues.Children.Add(lblTu);
                stpValues.Children.Add(txtSearch);
                txtSearch.Focus();
                txtSearch.TabIndex = 3;
            }
        }
        /// <summary>
        /// User: TAMNN 16/08/2014
        /// Sự kiện bàn phím
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void g_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            #region freeze column
            if ((((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) && ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)) && (e.Key == Key.F))
            {
                int columnDisplayIndex = g.CurrentCell.Column.DisplayIndex + 1;
                g.FrozenColumnCount = columnDisplayIndex;
            }
            #endregion
        }
        /// <summary>
        /// /Tạo mới nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTaoMoiNV_Click(object sender, RoutedEventArgs e)
        {
            var ThemNhanVien = new frmThemNhanVien();
            ThemNhanVien.DataContext = new  SSPharma.ViewModel.NhanSu.ThemNhanVienViewModel();
            ThemNhanVien.Show();
        }

        #endregion

        #region Utility function
        /// <summary>
        /// User: TAMNN 16/08/2014
        ///  Mục đích: Hàm lấy cấu trúc định dạng của lưới
        /// </summary>
        /// <param name="_tenform"></param>
        private void GetFormatGrid(string _tenform)
        {
            try
            {
                p_tDataCauTruc = _BizDSNhanSu.ft_DSDinhDangLuoi(_tenform);// lay cau truc bang hien thi
                UtilityUIElement.GenerateColumns(g, p_tDataCauTruc);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); 
            }
        }
        /// <summary>
        /// User: TAMNN 16/08/2014
        /// Mục đích: Lấy dữ liệu tương ứng với từng controll
        /// </summary>
        /// <param name="_tenform"></param>
        private void GetDataResponseMenu(string Depart,string OrgId,string p_sDieuKienTim="1=1")
        {
            try
            {
                p_tListData = _BizDSNhanSu.ft_GetListdata(Depart, OrgId, p_sDieuKienTim);// lay du lieu
                g.DataContext = p_tListData.AsDataView();
            }
            catch (Exception ex)
            { 
                MessageBox.Show( ex.Message.ToString());
            }
        }
        public IList<string> GetDataSearch()
        {
            int count = stpValues.Children.Count;
            UIElementCollection aUIRoot = stpValues.Children;
            IList<string> listValues = new List<string>();
            for (int i = 0; i < count; i++)
            {
                string name = "";
                try
                {
                    UIElement element = aUIRoot[i];
                    Type type = element.GetType();
                    if (type == typeof(TextBox))
                    {
                        if (((TextBox)element).Tag != null && !string.IsNullOrEmpty(((TextBox)element).Tag.ToString()))
                        {
                            name = ((TextBox)element).Tag.ToString();
                            listValues.Add((((TextBox)element).Text == null) ? null : ((((TextBox)element).Text.Trim() == "") ? null : ((TextBox)element).Text));
                        }
                    }
                    else if (type == typeof(AWTextBox))
                    {
                        if (((AWTextBox)element).Tag != null && !string.IsNullOrEmpty(((AWTextBox)element).Tag.ToString()))
                        {
                            name = ((AWTextBox)element).Tag.ToString();
                            listValues.Add((((AWTextBox)element).Text == null) ? null : ((((AWTextBox)element).Text.Trim() == "") ? null : ((AWTextBox)element).Text));
                        }
                    }
                    else if (type == typeof(AWDatePicker))
                    {
                        if (((AWDatePicker)element).Tag != null && !string.IsNullOrEmpty(((AWDatePicker)element).Tag.ToString()))
                        {
                            name = ((AWDatePicker)element).Tag.ToString();
                            listValues.Add((((AWDatePicker)element).Text == null) ? null : ((((AWDatePicker)element).Text.Trim() == "") ? null : ((AWDatePicker)element).Text));
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception(exception.Message + ": " + name);
                }
            }
            return listValues;
        }

        #endregion

        #region Các hàm cố định

        #endregion
        
        
    }
}
